package com.seeyou.lake.net

import com.seeyou.lake.bean.UserBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

/**
 * @des
 * @date 2021/9/24
 * @author fade
 */
class ServerRepository {

    //登录
    suspend fun loginByGoogle(
        userId: String,
        email: String,
        userAvatar: String,
        displayNames: String
    ): BaseResult<UserBean> =
        withContext(Dispatchers.IO) {
            val map = HashMap<String, Any>(2)
            map["userId"] = userId
            map["email"] = email
            map["userAvatar"] = userAvatar
            map["displayName"] = displayNames
            RetrofitCreator.server.loginByGoogle(map)
        }

    suspend fun getSecretStatus(userId: String) = withContext(Dispatchers.IO) {
        RetrofitCreator.server.getSecretStatus(userId)
    }

    suspend fun setSecret(userId: String,secret: String) = withContext(Dispatchers.IO) {
        RetrofitCreator.server.setSecret(userId,secret)
    }


    suspend fun getModelList() = withContext(Dispatchers.IO) {
        RetrofitCreator.server.getModelList()
    }

    suspend fun getArticleList(userId: String, type: Int) = withContext(Dispatchers.IO) {
        RetrofitCreator.server.getArticleList(userId, type)
    }

    suspend fun deleteArticle(pid: String) = withContext(Dispatchers.IO) {
        RetrofitCreator.server.deleteArticle(pid)
    }

    //上传图片
    suspend fun addArticle(
        uid: String,
        content: String,
        title: String,
        type: Int,
        secret: Int,
        path: String
    ) = withContext(Dispatchers.IO) {
        val requestBody: RequestBody =
            File(path).asRequestBody("image/png".toMediaTypeOrNull())
        val multiPart = MultipartBody.Part.createFormData("imageFile", File(path).name, requestBody)
        RetrofitCreator.server.addArticle(uid, content, title, type, secret, multiPart)
    }


    /*    //登录
        suspend fun loginByPass(username: String, password: String) = withContext(Dispatchers.IO) {
            val map = HashMap<String, Any>(2)
            map["username"] = username
            map["password"] = password
            RetrofitCreator.server.loginByPassword(map)
        }

        //注册
        suspend fun register(username:String,nickname:String,password:String,phone:String,header:String,sex:Int,age:Int)
                = withContext(Dispatchers.IO) {
            val map = HashMap<String, Any>(2)
            map["username"] = username
            map["nickname"] = nickname
            map["password"] = password
            map["phone"] = phone
            map["header"] = header
            map["sex"] = sex
            map["age"] = age
            RetrofitCreator.server.register(map)
        }

        suspend fun getFriendsList(userId: String) = withContext(Dispatchers.IO) {
            RetrofitCreator.server.getFriendsList(userId)
        }

        suspend fun getGroupList(userId: String) = withContext(Dispatchers.IO) {
            RetrofitCreator.server.getMyGroupList(userId)
        }

        suspend fun getRoomList(userId: String) = withContext(Dispatchers.IO) {
            RetrofitCreator.server.getRoomList(userId)
        }

        suspend fun getUnReadChatList(chatId:String,userId:String) = withContext(Dispatchers.IO) {
            RetrofitCreator.server.getUnReadChatList(chatId,userId)
        }


        suspend fun getUnReadGroupChatList(chatId:String,userId:String) = withContext(Dispatchers.IO) {
            RetrofitCreator.server.getUnReadGroupChatList(chatId,userId)
        }


        suspend fun addFriend(userId:String,friendId:String) = withContext(Dispatchers.IO) {
            RetrofitCreator.server.addFriend(userId,friendId)
        }

        suspend fun createGroup( requestBody: RequestBody)= withContext(Dispatchers.IO) {
            RetrofitCreator.server.createGroup(requestBody)
        }

        //上传图片
        suspend fun uploadHeader(path: String) = withContext(Dispatchers.IO) {
            val requestBody: RequestBody =
                File(path).asRequestBody("image/jpg".toMediaTypeOrNull())
            val multiPart = MultipartBody.Part.createFormData("imageFile", File(path).name, requestBody)
            RetrofitCreator.server.uploadHeader( multiPart)
        }*/
}